//
//  BaseTabBarController.m
//  HeWeiZhiNeng
//
//  Created by chengzhang Yan on 2017/11/30.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "BaseTabBarController.h"
#import "RTRootNavigationController.h"
#import "HomePageViewController.h"
#import "MapPageViewController.h"


@interface BaseTabBarController ()<UITabBarControllerDelegate, UITabBarDelegate>
@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:white_];
    
    //添加子控制器
    
    [self addAllChildViewControllers];
    
    self.tabBar.backgroundImage = [UIImage imageByColor:gray_one];
    
    self.delegate = self;
    
}

- (void)addAllChildViewControllers
{
    HomePageViewController *home = [[HomePageViewController alloc] init];
    [self addOneChildViewController:home title:@"首页" imageName:@"home_unselected" selectedImageName:@"home_selected"];
    
    MapPageViewController *map = [[MapPageViewController alloc] init];
    [self addOneChildViewController:map title:@"地图" imageName:@"map_unselected" selectedImageName:@"map_selected"];
}

//添加一个子控制器的具体实现方法
- (void)addOneChildViewController:(UIViewController *)childViewController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    //设置背景色
    /**
     *  这里设置背景色,会调用让ViewDidLoad里的方法走一笔,所以会出现偏差
     */
    //    childViewController.view.backgroundColor = YanCRandomColor;
    //    childViewController.view.backgroundColor = [UIColor whiteColor];
    //设置标签栏的标题
    childViewController.tabBarItem.title = title;
    //设置标签栏的按钮的图片
    childViewController.tabBarItem.image = [UIImage imageNamed:imageName];
    //设置标签栏按钮被选中后的图片  //UIImage 有个渲染模式属性,把这个关闭,可以显示原图,这样就不会显示渲染后的蓝色,显示原色,但是文字没有渲染色一说法
    childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //    childViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 5, 2, 5); //imageInsets这个属性设置之后,Item每点击一下都会变小.
    
    //添加导航控制器
    RTRootNavigationController *naviVC = [[RTRootNavigationController alloc] initWithRootViewController:childViewController];
    //设置导航栏标题
    
//    childViewController.navigationItem.title = title;
    
    [self addChildViewController:naviVC];
    
    //方法一:
    //设置代理来监控标签栏的按钮是否被点击,然后进行相应的删除按钮的背景图片,还有设置标签栏的字体的颜色也是和主题色保持一致,否则字体颜色是渲染的默认蓝色,当然这不是唯一的处理方法
    //    self.delegate = self;
    //方法二的思路:
    //还可以设置标签栏的整体的appearance,通过appearance对象,可以修改整个项目中所有的样式, 但是很遗憾: UITabBar 不提供 setTitleTextAttributes 的接口让我们使用,只提供了
    
    //也就是意味着,UITabBar不提供任何接口去修改字体,所以怎么修改字体和颜色呢?
    
    /**
     UITabBar *appearance = [UITabBar appearance];
     NSMutableDictionary *titleTextAttributes = [NSMutableDictionary dictionaryWithCapacity:0];
     titleTextAttributes[NSForegroundColorAttributeName] = YanCVioletColor; //字典笑笑语法赋值
     titleTextAttributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:22];
     NSShadow *shadow = [[NSShadow alloc] init];
     shadow.shadowOffset = CGSizeMake(0, 0);
     titleTextAttributes[NSShadowAttributeName] = shadow; //UIOffset已经被弃用,直接返回一个shadowOffset
     [appearance setTitleTextAttributes:titleTextAttributes];
     */
    
    //但是有一个让人高兴的事情是,UITabBarItem 有可以设置文字的接口
    
    UIBarItem *appearance = [UIBarItem appearance];
    //设置常态下
    NSMutableDictionary *titleTextAttributesForNormal = [NSMutableDictionary dictionaryWithCapacity:0];
    titleTextAttributesForNormal[NSForegroundColorAttributeName] = white_; //字典笑笑语法赋值
    titleTextAttributesForNormal[NSFontAttributeName] = [UIFont boldSystemFontOfSize:10];
    NSShadow *shadowForNormal = [[NSShadow alloc] init];
    shadowForNormal.shadowOffset = CGSizeMake(0, 0);
    titleTextAttributesForNormal[NSShadowAttributeName] = shadowForNormal; //UIOffset已经被弃用,直接返回一个shadowOffset
    [appearance setTitleTextAttributes:titleTextAttributesForNormal forState:(UIControlStateNormal)];
    
    //设置点击高亮状态下
    NSMutableDictionary *titleTextAttributesForHighlighted = [NSMutableDictionary dictionaryWithCapacity:0];
    titleTextAttributesForHighlighted[NSForegroundColorAttributeName] = blue_one; //字典笑笑语法赋值
    titleTextAttributesForHighlighted[NSFontAttributeName] = [UIFont boldSystemFontOfSize:10];
    NSShadow *Highlighted = [[NSShadow alloc] init];
    Highlighted.shadowOffset = CGSizeMake(0, 0);
    titleTextAttributesForHighlighted[NSShadowAttributeName] = Highlighted; //UIOffset已经被弃用,直接返回一个shadowOffset
    [appearance setTitleTextAttributes:titleTextAttributesForHighlighted forState:(UIControlStateSelected)];
    /**
     此时,代理可以不用添加,代理监听的点击UITabBarItem的方法也不用了,降低内存消耗
     */
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
